package com.tlgen.spring.utils;

import com.tlgen.spring.factory.SpeedDefaultListBeanFactory;
import com.tlgen.spring.model.BeanDefinition;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class SpeedSpringContextUtils<T> {

    public static <T> T getBean(Class<T> clazz) {
        List<BeanDefinition> beanDefinitionList = SpeedDefaultListBeanFactory.getInstance().getBeanDefinitionList();
        BeanDefinition beanDefinition = beanDefinitionList.stream()
                .filter(x -> Objects.equals(x.getClassName(), clazz.getName())).findFirst().orElse(null);
        if (Objects.isNull(beanDefinition))
            return null;
        return (T) beanDefinition.getInstance();
    }

    public static <T> List<T> getAllBean() {
        List<BeanDefinition> beanDefinitionList = SpeedDefaultListBeanFactory.getInstance().getBeanDefinitionList();
        return (List<T>) beanDefinitionList.stream().map(BeanDefinition::getInstance).collect(Collectors.toList());
    }

}
